package com.wangshb.agent.study.bytebuddy;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.FixedValue;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * 简单的拦截
 */
public class HelloWolrd {


    public String say() {
        return "i love you.";
    }

    public String say1() {
        return "i love you.";
    }


    public static void main(String[] args) throws Exception {

        Class<? extends HelloWolrd> loaded = new ByteBuddy()
                // 指定父类
                .subclass(HelloWolrd.class)
                // 指定拦截的方法
                .method(named("say").or(named("say1")))
                // 拦截方法调用，返回固定值
                .intercept(FixedValue.value("Hello world"))
                // 产生字节码
                .make()
                .load(HelloWolrd.class.getClassLoader())
                // 获取class对象
                .getLoaded();
        HelloWolrd instance = loaded.newInstance();
        System.out.println(instance.say());
        System.out.println(instance.say1());
    }
}
